package com.sc.test.recyclerview2;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.widget.Toast;

import com.sc.test.R;
import com.sc.test.RecyclerBaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Shen Chao on 2018-03-12-012 下午 21:15.
 */

public class LinearRvActivity extends RecyclerBaseActivity {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    private LinearRvAdapter adapter;
    private RecyclerView.LayoutManager layoutManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rv_linear);
        ButterKnife.bind(this);
        initData();
        initView();
    }

    private void initView() {
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置adapter
        recyclerView.setAdapter(adapter);
        //设置移除动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //设置间距
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }

    private void initData() {
        layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        adapter = new LinearRvAdapter(InitData.getData());

        adapter.setOnItemClickListener(new LinearRvAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(LinearRvActivity.this,
                        "click " + position + " item", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(LinearRvActivity.this,
                        "long click " + position + " item", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @OnClick({R.id.tv_add_item_btn, R.id.tv_del_item_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_add_item_btn:
                adapter.addNewItem();
                layoutManager.scrollToPosition(0);
                break;
            case R.id.tv_del_item_btn:
                adapter.deleteItem();
                layoutManager.scrollToPosition(0);
                break;
        }
    }
}
